import java.util.ArrayList;
import java.util.List;

public class Solution2 {
    //49. 字母异位词分组
    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> ret = new ArrayList<>();
        int n = strs.length;
        for (int i = 0; i < n; i++) {
            if (strs[i] == null) {
                continue;
            }
            List<String> list1 = new ArrayList<>();
            list1.add(strs[i]);
            for (int j = i + 1; j < n; j++) {
                if (isEquals(strs[i],strs[j])) {
                    list1.add(strs[j]);
                    strs[j] = null;
                }
            }
            ret.add(list1);
        }
        return ret;
    }
    public static boolean isEquals(String s1,String s2) {
        int[] hash = new int[26];

        if (s1 == null || s2 == null || s1.length() != s2.length()) {
            return false;
        }
        for (int i = 0; i < s1.length(); i++) {
            hash[s1.charAt(i)-'a']++;
            hash[s2.charAt(i)-'a']--;
        }
        for (int i = 0; i < 26; i++) {
            if (hash[i] != 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        groupAnagrams(strs);
    }
}
